clear all

*****************************************************************************
* ROLE OF ORGANIZATIONAL CAPITAL 
*****************************************************************************
clear 
use "${output_stata}\main_regression.dta" // Load main regression data

reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if share > 0.01 & share < ., absorb(orig_dest_year) cluster(apgr_1_num code_entry_num) 
gen sample = e(sample) // Generate sample variable for observations in regression
estimates store reg_1 // Store regression estimates
estadd local orig_dest_year "Yes" 
estadd local orig_dest_size "No"
estadd local ctrl "Yes"
estadd local orig1_orig2_dest "No"
estadd local orig_dest "No"
estadd local sirtg_num "No"
estadd local year "No"

replace admin_t1 = admin_t1 // Re-assign admin_t1
replace mkting_t1 = admin_t1 + mkting_t1 // Sum mkting_t1 and admin_t1
replace info_t1 = mkting_t1 + info_t1 // Sum info_t1 and mkting_t1

* Generate terciles for selected variables
local varlist rh_t1 mgmt_t1 hr_t1 admin_t1 info_t1 mkting_t1 
foreach x of local varlist {
    egen q_`x' = xtile(`x') if e(sample), nq(3) // Create terciles for each variable
    drop if q_`x' == . // Drop missing terciles
}

* Run regressions for each tercile of OrgCap variables
local varlist rh_t1 mgmt_t1 admin_t1 info_t1 mkting_t1 
foreach x of local varlist {
    di "`x'" // Display variable name

    di 1 // Display tercile indicator
    reghdfe buy HC_distance_s_brut_t1 /*
    */ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample & q_`x' == 1, absorb(orig_dest_year ) cluster(apgr_1_num code_entry_num) 
    estimates store reg_`x'_1 // Store regression for 1st tercile
    estadd local orig_dest_year "Yes"
    estadd local ctrl "Yes"

    di 2 // Display tercile indicator
    reghdfe buy HC_distance_s_brut_t1 /*
    */ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample & q_`x' == 2, absorb(orig_dest_year ) cluster(apgr_1_num code_entry_num) 
    estimates store reg_`x'_2 // Store regression for 2nd tercile
    estadd local orig_dest_year "Yes"
    estadd local ctrl "Yes"

    di 3 // Display tercile indicator
    reghdfe buy HC_distance_s_brut_t1 /*
    */ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample & q_`x' == 3, absorb(orig_dest_year ) cluster(apgr_1_num code_entry_num) 
    estimates store reg_`x'_3 // Store regression for 3rd tercile
    estadd local orig_dest_year "Yes"
    estadd local ctrl "Yes"
}

* Create dummy variable for hr_t1
gen dum_hr_t1 = hr_t1 > 0 & hr_t1 < . // Dummy variable for hr_t1

* Run regressions HR
reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample & dum_hr_t1 == 0, absorb(orig_dest_year ) cluster(apgr_1_num code_entry_num) 
estimates store reg_hr_t1_0 // Store regression for hr_t1 == 0
estadd local orig_dest_year "Yes"
estadd local ctrl "Yes"

reghdfe buy HC_distance_s_brut_t1 /*
*/ diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 if sample & dum_hr_t1 == 1, absorb(orig_dest_year ) cluster(apgr_1_num code_entry_num) 
estimates store reg_hr_t1_1 // Store regression for hr_t1 == 1
estadd local orig_dest_year "Yes"
estadd local ctrl "Yes"


esttab reg_rh_t1_1 reg_rh_t1_2 reg_rh_t1_3 reg_info_t1_1 reg_info_t1_2 reg_info_t1_3 reg_hr_t1_0 reg_hr_t1_1 using "${export}\orgcap.tex", /*
*/ keep(HC_distance_s_brut_t1) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/ alignment(D{.}{.}{-1})  /*     
*/ title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _) /* 
*/ collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest_year r2_a N, fmt(0 0 3 %15.0fc) /*
*/ labels(`"Controls"' `"Sector of Origin $\times$ Entry $\times$ Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/ layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) 
